//
//  ZGLNewsHeaderView.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/11/6.
//  Copyright © 2018 蒂埃里. All rights reserved.
//

#import "ZGLNewsHeaderView.h"
#import "ZGLNewsHeaderCollectionViewCell.h"

@interface ZGLNewsHeaderView ()
<UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) ZGLBannerView *banner;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end

@implementation ZGLNewsHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
        
        [self addSubview:self.banner];
        [self addSubview:self.collectionView];
        
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.matchArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"ZGLNewsHeaderCollectionViewCellID";
    ZGLNewsHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.model = self.matchArray[indexPath.row];
    
    
    return cell;
}




#pragma mark - setter&getter

- (void)setMatchArray:(NSArray<ZGLNewsBannerMatchModel *> *)matchArray
{
    _matchArray = matchArray;
    
}

- (void)setImageArray:(NSArray *)imageArray
{
    _imageArray = imageArray;
    [self.banner setupWithArray:imageArray];
}

- (ZGLBannerView *)banner
{
    if (!_banner) {
        _banner = [ZGLBannerView initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, self.bounds.size.height*0.6) hasTimer:YES interval:2.5 placeHolder:[UIImage imageNamed:@"loading"]];
        
    }
    return _banner;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height*0.6, UI_SCREEN_WIDTH, self.bounds.size.height*0.4) collectionViewLayout:self.layout];
        _collectionView.backgroundColor = ViewBackgroundColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollsToTop = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ZGLNewsHeaderCollectionViewCell class] forCellWithReuseIdentifier:@"ZGLNewsHeaderCollectionViewCellID"];
        
    }
    
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout
{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat itemW = IS_IPHONE_4and5?150:170;
        CGFloat h = IS_IPHONE_4and5?10:20;
        CGFloat itemH = self.bounds.size.height*0.4 - h;
        _layout.itemSize = CGSizeMake(itemW, itemH);
        _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        
    }
    return _layout;
}

@end
